package com.childmiles.gwt.client.domain;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class Award implements Serializable {
    // 64 x 64 images
    private static final String TEDDY_BEAR = "http://t3.gstatic.com/images?q=tbn:hyoLyhfRvzAlAM:http://i188.photobucket.com/albums/z153/NidiaE/teddy_bear.gif";
    private static final String WONDER_WOMAN = "http://i.i.tsatic-cdn.net/modules/forum/customavatars/avatar56608_1.gif";
    private static final String MERMAID = "/war/images/";
    
    private static final List<String> imageURLs = new ArrayList<String>();
    static {
        imageURLs.add("images/teddybear.jpg");
        imageURLs.add("/images/wonderwoman.gif");
        imageURLs.add("/images/ariel.jpg");
    }

    private String name;
	private String imageURL;
	private Date added;
	
	public Award() {
	    this("");
	}
	
	public Award(String name) {
	    this(name,imageURLs.get(getRandom()));
	}
	
	/**
	 * Can't use java.util.Random or the GWT version since Award is shared
	 * @return
	 */
    private static int getRandom() {
        return (int) (new Date().getTime() % imageURLs.size());
    }

    public Award(String name, String imageURL) {
        super();
        this.name = name;
        this.imageURL = imageURL;
        added = new Date();
    }
    
    public void setName(String name) {
        this.name = name;
    }
    public String getName() {
        return name;
    }
    public void setImageURL(String imageURL) {
        this.imageURL = imageURL;
    }
    public String getImageURL() {
        return imageURL;
    }

    /**
     * Switches the imageUrl to the next image in imageURLs
     * @return
     */
    public void changeToNextImage() {
        int newImageUrlIndex = (findCurentImageURLIndex() + 1) % imageURLs.size();
        imageURL = imageURLs.get(newImageUrlIndex);
    }

    private Integer findCurentImageURLIndex() {
        for (int i = 0; i < imageURLs.size(); i++) {
            if (imageURL.equals(imageURLs.get(i))) {
                return i;
            }
        }
        return null;
    }

    // The field (Date) added is the unique key so equals() is just the field added
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((added == null) ? 0 : added.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Award other = (Award) obj;
        if (added == null) {
            if (other.added != null)
                return false;
        } else if (!added.equals(other.added))
            return false;
        return true;
    }

    public void setAdded(Date added) {
        this.added = added;
    }

    public Date getAdded() {
        return added;
    }
    
    
}
 